import unittest

import requests

from api.invest_api import InvestApi
from api.reg_login_api import RegLoginApi
from common.util import build_account, html_parser, init_tender_info
from config import path_cfg


class TestInvest(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        init_tender_info()

    def setUp(self) -> None:
        self.session = requests.Session()
        self.reg_Login_api = RegLoginApi(self.session)
        self.invest_api = InvestApi(self.session)

        phone = path_cfg.REG_LOGIN_PHONE
        build_account('invest', self.session, phone)
        self.reg_Login_api.user_login({"keywords": phone, "password": "a123456"})

    def tearDown(self) -> None:
        self.session.close()

    # 投资
    def test01_invest(self):
        form_data = {
            "id": 642,
            "depositCertificate": -1,
            "amount": 500
        }
        resp = self.invest_api.invest(form_data)
        self.assertEqual(200, resp.status_code)
        self.assertEqual(200, resp.json().get('status'))

    # 第三方投资
    def test02_third_invest(self):
        form_data = {
            "id": 642,
            "depositCertificate": -1,
            "amount": 500
        }
        invest_resp = self.invest_api.invest(form_data)

        form_data = invest_resp.json().get('description').get('form')

        parser_data = html_parser(form_data)

        third_invest_resp = self.invest_api.third_invest(parser_data.get('url'), parser_data.get('req_body'))
        self.assertIn('OK', third_invest_resp.text)
